//
//  GCFBaseViewController.m
//  ZhouDao
//
//  Created by hyj on 2018/5/18.
//  Copyright © 2018年 hyj. All rights reserved.
//

#import "XWBaseViewController.h"
#import <TYUIKit/TYUIKit.h>
//#import <Masonry/Masonry.h>
#import "UIButton+BackgroundColorWithState.h"
#import <YYCategories/YYCategories.h>
#import <CommonTools/CommonTools-Swift.h>
#import <CommonTools/ZHHomeLog.h>

@implementation RightIconInfo
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.color = TY_RGBAColor(38, 36, 53, 1);
        self.bgColor = [UIColor whiteColor];
        self.radian = 0;
    }
    return self;
}
@end

@interface XWBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation XWBaseViewController


- (void)dealloc
{
    if (XWClassRoomManager.logType != XWClassRoomLogTypeNone){
        ZHLog(@"%@页面销毁了",[self class]);
    }
}

-(void)rightAction:(UIButton*)sender
{
    
}

-(void)leftAction:(UIButton*)sender
{
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)setTitle:(NSString *)title rightTitle:(nullable NSString * )rightTitle rightImage:(nullable NSString * )rightImage
{
    self.navigationItem.title = title;
    if (self.navigationController.viewControllers.count > 1 || self.presentingViewController) {
        UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.frame = CGRectMake(0, 0, 40, 44);
        
        [backBtn setImage:[[UIImage imageWithBundleName:@"CommonTools/app_back_black"]imageByResizeToSize:CGSizeMake(TY_ScreenAdaptionLength(24), TY_ScreenAdaptionLength(24))] forState:UIControlStateNormal];
//        [backBtn setImage:[[UIImage imageNamed:@"导航栏_返回键"] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        self.navigationItem.leftBarButtonItem = backItem;
    }
    if (rightImage.length || rightTitle.length) {
        
        UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 30)];
        [btn setTitle:rightTitle forState:UIControlStateNormal];
        self.rightButton = btn;
        [btn setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal];
        [btn setTitleColor:TY_RGBAColor(38, 36, 53, 1) forState:UIControlStateNormal];
        [btn setTitleColor:TY_RGBAColor(38, 36, 53, 0.7) forState:UIControlStateHighlighted];
        [btn setTitleColor:TY_RGBAColor(38, 36, 53, 0.5) forState: UIControlStateDisabled];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:TY_ScreenAdaptionLength(15)];
        CGSize size = [rightTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 44) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:TY_ScreenAdaptionLength(15)]} context:nil].size;
        CGFloat width = size.width;
        if (width >30){
            btn.ty_width = width;
        }
        
        self.rightButton.ty_right = self.view.ty_width ;
        [btn setTitle:rightTitle forState:UIControlStateNormal];
        
        if (rightImage.length) {
            UIImage* image = [UIImage imageWithBundleName:rightImage];
            [btn setImage:image forState:UIControlStateNormal];
            btn.ty_size = image.size;
        }
        [btn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    }
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
//    self.tabBarController.navigationController.view.backgroundColor = [UIColor blueColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.navigationItem.title ) {
        [self setTitle:self.navigationItem.title];
//        [self setTitle:self.navigationItem.title rightTitle:self.rightButton.currentTitle rightImage:nil];
    }
    [self forbiddenSideBack];
    self.navigationController.navigationBar.standardAppearance.backgroundColor = self.view.backgroundColor;
}

-(void)forbiddenSideBack
{
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.navigationController.interactivePopGestureRecognizer.delegate = (self.navigationController.viewControllers.count == 1 ? self : nil);
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
{
    if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        return self.navigationController.interactivePopGestureRecognizer.delegate != self;
    }
    return YES;
}

//创建tableView
-(void)configureTableView
{
    [self configureTableViewWithStyle:UITableViewStylePlain];
}

-(void)configureTableViewWithStyle:(UITableViewStyle)style{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:style];
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [UIColor ty_colorWithHex:0xededed];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.backgroundColor = [UIColor ty_colorWithHex:0xefeff4];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    
//    @weakify(self);
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        @strongify(self);
//        make.edges.equalTo(self.view);
//    }];
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}


#pragma mark - UITableViewDatasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark - UITableViewDelegate

/**
 *   适配ios11，出现section变高的问题
 */

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return tableView.style == UITableViewStyleGrouped ? 0.01 : 0.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return tableView.style == UITableViewStyleGrouped ? 0.01 : 0.0;
}

/** 收键盘 */
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

@end
